package Classic150;

public class 罗马数字转整数 {
    /*
        纯模拟，多想想逻辑
     */
    public int romanToInt(String s) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            int num = getValve(s.charAt(i));
            if (i < s.length() - 1 && num < getValve(s.charAt(i + 1))) sum -= num;
            else sum += num;
        }
        return sum;
    }

    private int getValve(char x) {
        return switch (x) {
            case 'I' -> 1;
            case 'V' -> 5;
            case 'X' -> 10;
            case 'L' -> 50;
            case 'C' -> 100;
            case 'D' -> 500;
            case 'M' -> 1000;
            default -> 0;
        };
    }
}
